package com.edvard.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import cn.kuaipan.android.openapi.KuaipanAPI;
import cn.kuaipan.android.sdk.exception.KscException;
import cn.kuaipan.android.sdk.exception.KscRuntimeException;

import com.edvard.jsbackup.R;

/**
 * 网盘中创建文件夹的类
 * 包含了异步任务的代码
 * 
 * @author edvard
 *
 */
public class MkdirTask {

    private static final String TAG = "MkdirTask";

    private ProgressDialog dialog;
    private Activity activity;

    public MkdirTask(Activity activity) {
        this.activity = activity;
    }
    
    /**
     * 显示进度条代码
     * @param msg
     */
    public void showProgressDialog(String msg) {
        dialog = new ProgressDialog(activity);
        dialog.setMessage(msg);
        dialog.setCancelable(false);
        dialog.show();
    }
    
    /**
     * 隐藏进度条代码
     */
    public void hideProgressDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
    
    /**
     * 显示警告弹出框
     * @param title
     * @param message
     */
    protected void displayAlert(String title, String message) {

        AlertDialog.Builder confirm = new AlertDialog.Builder(activity);
        confirm.setTitle(title);
        confirm.setMessage(message);

        confirm.setNegativeButton(activity.getString(R.string.ok),
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                    }
                });

        confirm.show().show();
    }
    
    /**
     * 显示错误弹出框
     * @param title
     * @param message
     */
    protected void displayErrorAlert(String title, String message) {

        AlertDialog.Builder confirm = new AlertDialog.Builder(activity);
        confirm.setTitle(title);
        confirm.setMessage(message);

        confirm.setNegativeButton(activity.getString(R.string.ok),
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {

                        activity.finish();
                    }
                });

        confirm.show().show();
    }
    
    /**
     * 开始执行创建文件夹任务
     * @param req
     */
    public void start(RequestBase req) {
        new Mkdir().execute(req);
    }
    
    /**
     * 创建文件夹
     * @author edvard
     */
    private class Mkdir extends AsyncTask<RequestBase, Void, ResultBase> {
    	KuaipanAPI api;
        protected void onPreExecute() {
            showProgressDialog("正在创建文件夹...");
        }

        protected ResultBase doInBackground(RequestBase... params) {
            Log.v(TAG, "====mkdir task doInBackground====");
            if (params == null || params.length != 1) {
                return null;
            }

            final RequestBase req = (RequestBase) params[0];
            final String remotepath = req.getRemotePath();
            api = req.getApi();

            ResultBase result = new ResultBase();

            result.setRemotePath(remotepath);
            Log.v(TAG, "remotepath:["+remotepath+"], you want to create");
            try {
                api.mkdirs(remotepath);
            } catch (KscRuntimeException e) {
                e.printStackTrace();
                result.setErrorMsg(e.toString());
                Log.v(TAG, "KscRuntimeException:"+e.toString());
            } catch (KscException e) {
                e.printStackTrace();
                result.setErrorMsg(e.toString());
                Log.v(TAG, "KscException:"+e.toString());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            Log.v(TAG, "====mkdir task doInBackground end====");
            return result;

        }

        protected void onPostExecute(ResultBase result) {

            hideProgressDialog();
            if (result == null) {
                Toast.makeText(activity,
                        "mkdir failed",
                        Toast.LENGTH_LONG).show();
                return;
            }
            if (result.getErrorMsg() != null) {
//                displayErrorAlert("Mkdir",
//                        result.getErrorMsg());
            	displayErrorAlert("创建文件夹", "创建文件夹失败");
            } else {
                displayAlert("创建文件夹信息","文件夹创建成功");

            }
        }
    }
}
